from typing import Annotated

from fastapi import APIRouter, Depends
from fastapi.params import Path
from pydantic import StringConstraints

from app.core.handlers.response import BaseResponse
from app.user.models.role import Role, RoleCreate
from app.user.models.user import UserCreate, UserPublic
from app.user.services import RoleService, UserService

user_router = APIRouter(
    prefix="/user", tags=["Users"], responses={404: {"description": "Not found"}}
)

role_router = APIRouter(
    prefix="/role", tags=["Roles"], responses={404: {"description": "Not found"}}
)


@user_router.get(
    "/{user_id}", summary="获取单个用户", response_model=BaseResponse[UserPublic]
)
async def get_user(user_id: int, user_service: UserService = Depends()):
    """获取单个用户"""
    user_data = await user_service.get_user(user_id)

    return user_data


@user_router.get(
    "/list/{user_ids}",
    summary="批量获取用户",
    response_model=BaseResponse[list[UserPublic]],
)
async def get_users(
    user_ids: Annotated[
        str,
        StringConstraints(pattern=r"^\d+(,\d+)*$", strip_whitespace=True),
        Path(description="用户ID列表，多个用英文逗号分隔, 如: list/1,2,3"),
    ],
    user_service: UserService = Depends(),
):
    """批量获取用户"""
    user_data = await user_service.get_users(user_ids)

    return user_data


@user_router.post(
    "",
    summary="创建新角色",
    description="创建一个新的系统角色，需要提供角色名称和描述信息",
    response_model=BaseResponse[UserPublic],
)
async def create_user(user_data: UserCreate, user_service: UserService = Depends()):
    """创建用户"""
    _data = await user_service.create_user(user_data)

    return _data


# @user_router.post("/user-roles/link",
#                   summary="用户角色关联",
#                   description="用户角色关联",
#                   response_model=BaseResponse[NoneType])
# async def link_role_to_user(
#         link_data: UserRoleLinkRequest,
#         user_role_service: UserRoleService = Depends()) -> Optional[Dict]:
#     """
#     关联用户和角色
#     Request Body:
#     - user_id: str (必填）
#     - role_ids: str (必填)
#     """
#     # 调用服务层方法
#     result = await user_role_service.link_role_to_user(link_data.user_id, link_data.role_ids)
#     return result
#
#
# @user_router.post("/user-roles/unlink",
#                   summary="取消关联用户和角色",
#                   description="取消关联",
#                   response_model=BaseResponse[NoneType])
# async def unlink_user_from_role(
#         link_data: UserRoleLinkRequest,
#         user_role_service: UserRoleService = Depends()) -> Dict:
#     """
#     取消用户和角色的关联
#     Request Body:
#     - user_id: str (必填）
#     - role_ids: str (必填)
#     """
#     # 调用服务层方法
#     result = await user_role_service.unlink_user_role(link_data.user_id, link_data.role_ids)
#     return result
#


@role_router.get(
    "/{role_id}", summary="获取用户角色", response_model=BaseResponse[Role]
)
async def get_user_roles(role_id: int, role_service: RoleService = Depends()):
    """获取用户角色"""

    role_data = await role_service.get_role(role_id)

    return role_data


@role_router.post("")
async def create_role(role_data: RoleCreate, role_service: RoleService = Depends()):
    """创建角色"""
    _data = await role_service.create_role(role_data)

    return _data
